package com.sinsoft.monitorDemo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinsoft.monitorDemo.dao.DiskInfoDao;
import com.sinsoft.monitorDemo.dao.ServerDao;
import com.sinsoft.monitorDemo.entity.DiskInfo;
import com.sinsoft.monitorDemo.entity.ServerInfo;

@Controller
@RequestMapping("/diskInfo")
public class DiskInfoController {
	
	/**
	 * 得到当前时间之前一段时间的的磁盘信息
	 * @return
	 */
	@RequestMapping(value = "/getDiskinfoList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getDiskinfoList(HttpServletRequest request){
		DiskInfoDao dao = new DiskInfoDao();
		String ip = request.getParameter("ip");
		List<Map<String, DiskInfo>> diskInfo = dao.getDiskInfoList(ip);
		ResponseData responseData = new ResponseData(true, diskInfo);
		return responseData;	
	}
	
	/**
	 * 得到最新的磁盘信息
	 * @return
	 */
	@RequestMapping(value = "/getNewDiskinfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getNewDiskInfo(HttpServletRequest request){
		String ip = request.getParameter("ip");
		DiskInfoDao dao = new DiskInfoDao();
		Map<String, DiskInfo> diskInfo = dao.getNewDiskInfo(ip);
		ResponseData responseData = new ResponseData(true, diskInfo);
		return responseData;	
	}
	/**
	 * 得到多个IP磁盘信息
	 * @return
	 */
	@RequestMapping(value = "/getDiskinfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getDiskInfo(HttpServletRequest request){
		String ips = request.getParameter("ip");
//		String ips = "10.10.200.226,10.10.200.228";
		String[] ipArray = ips.split(",");
		DiskInfoDao dao = new DiskInfoDao();
		Map<String, Object> diskInfoTotal = new HashMap<String, Object>();
		for (String ip : ipArray) {		
			Map<String, DiskInfo> diskInfo = dao.getNewDiskInfo(ip);
			diskInfoTotal.put(ip, diskInfo);
		}
		ResponseData responseData = new ResponseData(true, diskInfoTotal);
		return responseData;	
	}
	/**
	 * 得到所有IP磁盘信息
	 * @return
	 */
	@RequestMapping(value = "/getAllDiskinfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getAllDiskInfo(){
		DiskInfoDao dao = new DiskInfoDao();
		ServerDao serverDao = new ServerDao();
		List<ServerInfo> serverInfos = serverDao.getAllServerInfo();
		Map<String, Object> diskInfos = new HashMap<String, Object>();
		for (ServerInfo serverInfo : serverInfos) {
			String ip = serverInfo.getIp();
			if(ip.contains(",")){
				String[] ips = ip.split(",");
				for (int i = 0; i < ips.length; i++) {
					Map<String, DiskInfo> newDiskInfo = dao.getNewDiskInfo(ips[i]);
					diskInfos.put(ips[i], newDiskInfo);	
				}
			}else {
				Map<String, DiskInfo> newDiskInfo = dao.getNewDiskInfo(ip);
				diskInfos.put(ip, newDiskInfo);	
			}
		}
		ResponseData responseData = new ResponseData(true, diskInfos);
		return responseData;	
	}
}
